<?php 
	class  Survey extends Model{
		private $_Options = array();
		private $_TotalVotes;

		function __construct($arrInitialValues = null){
			parent::__construct();
			
			$this->setTable('surveys');
			$this->setColumnId('survey_id');
			
			$this->_setDefaultValues($arrInitialValues);
			parent::__construct($arrInitialValues);
		}
		
		
		private function _setDefaultValues($arrInitialValues = null){
			if (isset($arrInitialValues['options']))
				$this->setOptions($arrInitialValues['options']);
			else
				$this->setOptions(array());
		}
		
		
		
		public function setOptions($value){
			$this->_Options = $value;
		}

		/**
		 * @return the $_TotalVotes
		 */
		public function getTotalVotes() {
			return $this->_TotalVotes;
		}
	
		/**
		 * @param field_type $_TotalVotes
		 */
		public function setTotalVotes($_TotalVotes) {
			$this->_TotalVotes = $_TotalVotes;
		}

		public function getOptions(){
			return $this->_Options;
		}
		
		public function answers($option, $survey){
			$query = '
				SELECT COUNT(1) AS total
				FROM answers
				WHERE survey_id = ' . $survey . '
					AND option_id = ' . $option . '
				GROUP BY option_id';
			
			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
		public function Vote($option, $ip){
			$query = '
				INSERT INTO answers
				(option_id, survey_id, ip)
				VALUES (' . $option . ', ' . $this->getId() . ', "' . $ip . '")';
			$dbHelper = new DBHelper($query);
		}
		
		public function getNumberVotes(){
			$query = '
				SELECT COUNT(1) AS total
				FROM answers
				WHERE survey_id = ' . $this->getId();
			
			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
		public function hasVoted($ip){
			$query = '
				SELECT COUNT(1) AS voted
				FROM answers
				WHERE survey_id = ' . $this->getId() . ' 
					AND ip = "' . $ip . '"';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
		public function userHasVoted($ip){
			$vote = $this->hasVoted($ip);
			return ($vote[0]["voted"] > 0);
		}
		
		public function getVotes(){
			$query = '
				SELECT an.option_id, description, COUNT(1) AS votos
				FROM answers AS an
				INNER JOIN options AS op ON op.option_id = an.option_id
				WHERE an.survey_id = ' . $this->getId() . '
				GROUP BY an.option_id, an.survey_id';
			
			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
		public function getCurrent(){
			$query = '
				SELECT *
				FROM surveys
				WHERE active = 1
				ORDER BY ' . $this->getColumnId() . ' DESC
				LIMIT 0, 1';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}

		public function getMyOptions(){
			$allTheOptions = array();
			
			$options = $this->getTheOptions();
			
			foreach ($options as $option){
				$oneOption = new stdClass();
				$oneOption->description = $option['description'];
				$oneOption->id = $option['option_id'];
				$allTheOptions[] = $oneOption;
			}
			
			$this->setOptions($allTheOptions);
		}
		
		public function getTheOptions(){
			$query = '
				SELECT *
				FROM options
				WHERE survey_id = ' . $this->getId() . '
				AND active = 1';
			
			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
		private function getLastActivatedSurvey(){
			$query = '
				SELECT *
				FROM surveys
				WHERE last_activated = 1';
			
			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();	
		}
		
		public function getLastSurvey(){
			$survey = $this->getLastActivatedSurvey();
			$this->setId($survey[0]["survey_id"]);
			$this->setDescription($survey[0]["description"]);
		}

	}
?>